/*
	xmlOutputWriter.cxx
	--------------------
	
	Author: Olivier Sputael
	
	This file is the class functions that will generate a xml output file.
*/

#include <sl/xmlOutputWriter.hxx>

#define MY_ENCODING "ISO-8859-1"

/**
  * @param filename the output file
  *
  * Initializes the document
  */
XmlOutputWriter::XmlOutputWriter (char* filename) {
	LIBXML_TEST_VERSION;
	writer = xmlNewTextWriterFilename(filename, 0);
	xmlTextWriterSetIndent(writer, 4);
	
	if (writer == NULL)
		cerr << "Error initializing output file !" << endl;
	if (xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL) < 0)
		cerr << "Error starting the document" << endl;
	// writing the first tag: <dSL_program>
	if (xmlTextWriterStartElement(writer, BAD_CAST "dSL_program") < 0)
		cerr << "Error starting new tag (dSL_program) in xml intermediate file" << endl;
}

/**
  * Do nothing !
  */
XmlOutputWriter::~XmlOutputWriter () {}

/**
  * Closes the document
  */
 void XmlOutputWriter::endWriting () {
	if (xmlTextWriterEndDocument(writer) < 0) {
		cerr << "Error closing document" << endl;
	}
	
	xmlFreeTextWriter(writer);
	xmlCleanupParser();
}

/**
  * @param in the string to convert
  * @param encoding the encoding type
  *
  * @return the converted string
  *
  * Converts a string in a xmlChar* with the specified encoding
  */
xmlChar* XmlOutputWriter::ConvertInput(char *in, const char *encoding) {
    xmlChar *out;
    int ret;
    int size;
    int out_size;
    int temp;
    xmlCharEncodingHandlerPtr handler;

    if (in == 0)
        return 0;

    handler = xmlFindCharEncodingHandler(encoding);

    if (!handler) {
        printf("ConvertInput: no encoding handler found for '%s'\n",
               encoding ? encoding : "");
        return 0;
    }

    size = (int) strlen(in) + 1;
    out_size = size * 2 - 1;
    out = (unsigned char *) xmlMalloc((size_t) out_size);

    if (out != 0) {
        temp = size - 1;
        ret = handler->input(out, &out_size, (const xmlChar *) in, &temp);
        if ((ret < 0) || (temp - size + 1)) {
            if (ret < 0) {
                printf
				("ConvertInput: conversion wasn't successful.\n");
            } else {
                printf
				("ConvertInput: conversion wasn't successful. converted: %i octets.\n",
                     temp);
            }

            xmlFree(out);
            out = 0;
        } else {
            out = (unsigned char *) xmlRealloc(out, out_size + 1);
            out[out_size] = 0;  /*null terminating out */
        }
    } else {
        printf
		("ConvertInput: no mem\n");
    }

    return out;
}

/**
  * @param opcode the opcode id
  *
  * @return the opcode text
  *
  * Returns the opcode text related to the given opcode id 
  */
char* XmlOutputWriter::getOpcodeText (int opcode) {
	switch(opcode) {
		case DSL_INTERMEDIATE_OPCODE_PROGRAM:				return "PROGRAM";
		case DSL_INTERMEDIATE_OPCODE_TYPE:					return "TYPE";
		case DSL_INTERMEDIATE_OPCODE_STRUCT_FIELD:			return "FIELD";
		case DSL_INTERMEDIATE_OPCODE_GLOBAL_VARIABLE:		return "GLOBAL VARIABLE";
		case DSL_INTERMEDIATE_OPCODE_INSTRUCTION:			return "INSTRUCTION";
		case DSL_INTERMEDIATE_OPCODE_OPERATION: 			return "OPERATION";
		case DSL_INTERMEDIATE_OPCODE_FUNCTION_ENTRY: 		return "FUNCTION";
		case DSL_INTERMEDIATE_OPCODE_SEQUENCE_ENTRY:		return "SEQUENCE";
		case DSL_INTERMEDIATE_OPCODE_START_NODE:			return "START NODE";
		case DSL_INTERMEDIATE_OPCODE_CFG:					return "START CFG";
		default:											return "?????????";
	}
}

/**
  * @param simple_type the simple type number
  *
  * Returns the simple type name as defined in sl/koftype.hxx
  */
char* XmlOutputWriter::getSimpleTypeText (int simple_type) {
	switch (simple_type) {
		case KIND_OF_SIMPLE_TYPE_UNKNOWN:			return "KIND_OF_SIMPLE_TYPE_UNKNOWN";
		case KIND_OF_SIMPLE_TYPE_BOOL:				return "KIND_OF_SIMPLE_TYPE_BOOL";
		case KIND_OF_SIMPLE_TYPE_SINT:				return "KIND_OF_SIMPLE_TYPE_SINT";
		case KIND_OF_SIMPLE_TYPE_INT:				return "KIND_OF_SIMPLE_TYPE_INT";
		case KIND_OF_SIMPLE_TYPE_DINT:				return "KIND_OF_SIMPLE_TYPE_DINT";
		case KIND_OF_SIMPLE_TYPE_LINT:				return "KIND_OF_SIMPLE_TYPE_LINT";
		case KIND_OF_SIMPLE_TYPE_USINT:				return "KIND_OF_SIMPLE_TYPE_USINT";
		case KIND_OF_SIMPLE_TYPE_UINT:				return "KIND_OF_SIMPLE_TYPE_UINT";
		case KIND_OF_SIMPLE_TYPE_UDINT:				return "KIND_OF_SIMPLE_TYPE_UDINT";
		case KIND_OF_SIMPLE_TYPE_ULINT:				return "KIND_OF_SIMPLE_TYPE_ULINT";
		case KIND_OF_SIMPLE_TYPE_REAL:				return "KIND_OF_SIMPLE_TYPE_REAL";
		case KIND_OF_SIMPLE_TYPE_LREAL:				return "KIND_OF_SIMPLE_TYPE_LREAL";
		case KIND_OF_SIMPLE_TYPE_TIME:				return "KIND_OF_SIMPLE_TYPE_TIME";
		case KIND_OF_SIMPLE_TYPE_DATE:				return "KIND_OF_SIMPLE_TYPE_DATE";
		case KIND_OF_SIMPLE_TYPE_TIME_OF_DAY:		return "KIND_OF_SIMPLE_TYPE_TIME_OF_DAY";
		case KIND_OF_SIMPLE_TYPE_DATE_AND_TIME:		return "KIND_OF_SIMPLE_TYPE_DATE_AND_TIME";
		case KIND_OF_SIMPLE_TYPE_STRING:			return "KIND_OF_SIMPLE_TYPE_STRING";
		case KIND_OF_SIMPLE_TYPE_BYTE:				return "KIND_OF_SIMPLE_TYPE_BYTE";
		case KIND_OF_SIMPLE_TYPE_WORD:				return "KIND_OF_SIMPLE_TYPE_WORD";
		case KIND_OF_SIMPLE_TYPE_DWORD:				return "KIND_OF_SIMPLE_TYPE_DWORD";
		case KIND_OF_SIMPLE_TYPE_LWORD:				return "KIND_OF_SIMPLE_TYPE_LWORD";
		case KIND_OF_SIMPLE_TYPE_ENUM:				return "KIND_OF_SIMPLE_TYPE_ENUM";
		case KIND_OF_SIMPLE_TYPE_POINTER:			return "KIND_OF_SIMPLE_TYPE_POINTER";
		case KIND_OF_SIMPLE_TYPE_LIST:				return "KIND_OF_SIMPLE_TYPE_LIST";
		case KIND_OF_SIMPLE_TYPE_ANYSIMPLE:			return "KIND_OF_SIMPLE_TYPE_ANYSIMPLE";
		case KIND_OF_SIMPLE_TYPE_ANYNUMBER:			return "KIND_OF_SIMPLE_TYPE_ANYNUMBER";
		default:									return "ERROR_UNKNOWN_SIMPLE_TYPE";
	}
}

/**
  * @param kind_of_type 
  *
  * Returns the kind of type as defined in sl/koftype.hxx
  */
char* XmlOutputWriter::getKindOfTypeText (int kind_of_type) {
	switch (kind_of_type) {
		case KIND_OF_TYPE_UNKNOWN:				return "KIND_OF_TYPE_UNKNOWN";
		case KIND_OF_TYPE_BUILT_IN_SIMPLE:		return "KIND_OF_TYPE_BUILT_IN_SIMPLE";
		case KIND_OF_TYPE_DERIVED:				return "KIND_OF_TYPE_DERIVED";
		case KIND_OF_TYPE_RANGE:				return "KIND_OF_TYPE_RANGE";
		case KIND_OF_TYPE_STRUCTURE:			return "KIND_OF_TYPE_STRUCTURE";
		case KIND_OF_TYPE_ARRAY:				return "KIND_OF_TYPE_ARRAY";
		case KIND_OF_TYPE_ENUM:					return "KIND_OF_TYPE_ENUM";
		case KIND_OF_TYPE_POINTER:				return "KIND_OF_TYPE_POINTER";
		case KIND_OF_TYPE_LIST:					return "KIND_OF_TYPE_LIST";
		case KIND_OF_TYPE_BLOCK:				return "KIND_OF_TYPE_BLOCK";
		default:								return "ERROR_UNKNOWN_KIND_OF_TYPE";
	}
}

/**
  * @param src the integer to convert
  *
  * @return the converted string
  *
  * Converts from int to char*
  */
char* XmlOutputWriter::intToCharPtr (int src) {
	char* res = new char[sizeof(src)];
	sprintf(res, "%d", src);
	
	return res;
}

/**
  * @param src: the long integer to convert
  *
  * @return the converted string
  *
  * Converts long to char*
  */
char* XmlOutputWriter::longToCharPtr (long src) {
	char* res = new char[sizeof(src)];
	sprintf(res, "%ld", src);
	
	return res;
}

/**
  * @param src the input to convert
  *
  * @return the converted string
  *
  * Converts from UNV_SHORT to char*
  */
char* XmlOutputWriter::unvShortToCharPtr (UNV_SHORT* src) {
	char* res = new char[2];
	sprintf(res, "%hu", ((*src)[ 0 ] * 0x100 + (*src)[ 1 ]));
	
	return res;
}

/**
  * @param src the input to convert
  *
  * @return the converted string
  *
  * Converts from UNV_LONG to char*
  */
char* XmlOutputWriter::unvLongToCharPtr (UNV_LONG* src) {
	char* res = new char[sizeof(src)];
	unsigned long result;
	result = ((*src)[ 0 ] * 0x100L + (*src)[ 1 ]);
	result = result * 0x10000L + ((*src)[ 2 ] * 0x100L + (*src)[ 3 ]);
	sprintf(res, "%lu", result);
	
	return res;
}

/**
  * @param src the char to convert
  *
  * @return the converted string
  *
  * Converts from char to char*
  */
char* XmlOutputWriter::charToCharPtr (char src) {
	char* res = new char[1];
	sprintf(res, "%c", src);
	
	return res;
}

void XmlOutputWriter::generate_xml_debug (char* debug_message) {
	xmlTextWriterStartElement(writer, BAD_CAST "DEBUG");
	xmlTextWriterWriteString(writer, ConvertInput(debug_message, MY_ENCODING));
	xmlTextWriterEndElement(writer);
}

/**
  * Generates the closing tag of the last openend tag
  */
void XmlOutputWriter::generate_xml_tag_end () {
	if (xmlTextWriterEndElement(writer) < 0)
		cerr << "generate_xml_tag_end: Error closing tag in xml intermediate file" << endl;
}

/**
  * @param opcode the opcode id
  * @param comment the comment to write in the header
  * (other params are line numbers and positions in the source code)
  *
  * Generates the header block
  */
void XmlOutputWriter::generate_xml_header (char* tagname, int opcode, char* comment, UNV_SHORT& ln_start_1, UNV_SHORT& ln_end_1, UNV_SHORT& ln_start_2, 
											UNV_SHORT& ln_end_2, UNV_LONG& pos_start_1, UNV_LONG& pos_end_1, UNV_LONG& pos_start_2, 
											UNV_LONG& pos_end_2) {
	// <header>
	if (xmlTextWriterStartElement(writer, ConvertInput(tagname, MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing new tag (header) in xml intermediate file" << endl;
	// <opcode id=".." name="..">...</opcode>
	if (xmlTextWriterStartElement(writer, BAD_CAST "opcode") < 0)
		cerr << "generate_xml_header: Error writing new tag (opcode) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(intToCharPtr(opcode), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(DSL_OPCODES_NAME[opcode], MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(getOpcodeText(opcode), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing text (opcode_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <comment>...</comment>
	if (xmlTextWriterStartElement(writer, BAD_CAST "comment") < 0)
		cerr << "generate_xml_header: Error writing new tag (comment) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(comment, MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing text (comment_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <line_number start_1=".." end_1=".." start_2=".." end_2=".." />
	if (xmlTextWriterStartElement(writer, BAD_CAST "line_number") < 0)
		cerr << "generate_xml_header: Error writing new tag (line_number) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "start_1", ConvertInput(unvShortToCharPtr(&ln_start_1), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (line_num_start_1) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "end_1", ConvertInput(unvShortToCharPtr(&ln_end_1), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (line_num_end_1) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "start_2", ConvertInput(unvShortToCharPtr(&ln_start_2), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (line_num_start_2) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "end_2", ConvertInput(unvShortToCharPtr(&ln_end_2), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (line_num_end_2) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <position begin_1=".." end_1=".." begin_2=".." end_2=".." />
	if (xmlTextWriterStartElement(writer, BAD_CAST "position") < 0)
		cerr << "generate_xml_header: Error writing new tag (position) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "begin_1", ConvertInput(unvLongToCharPtr(&pos_start_1), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (pos_begin_1) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "end_1", ConvertInput(unvLongToCharPtr(&pos_end_1), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (pos_end_1) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "begin_2", ConvertInput(unvLongToCharPtr(&pos_start_2), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (pos_begin_2) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "end_2", ConvertInput(unvLongToCharPtr(&pos_end_2), MY_ENCODING)) < 0)
		cerr << "generate_xml_header: Error writing attribute (pos_end_2) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// </header>
	generate_xml_tag_end();
}

/**
  * @param name: the name of the type
  * @param kind: the kind of the type
  * @param type_id: the id of the type
  *
  * Generates the header block for types
  */
void XmlOutputWriter::generate_xml_common_type_header (char* name, int kind, UNV_LONG& type_id) {
	// <common_type_header name=".." kind_id=".." kind=".." type_id=".." />
	if (xmlTextWriterStartElement(writer, BAD_CAST "common_type_header") < 0)
		cerr << "generate_xml_common_type_header: Error writing new tag (common_type_header) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_common_type_header: Error writing attribute (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "kind_id", ConvertInput(intToCharPtr(kind), MY_ENCODING)) < 0)
		cerr << "generate_xml_common_type_header: Error writing attribute (kind_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "kind", ConvertInput(getKindOfTypeText(kind), MY_ENCODING)) < 0)
		cerr << "generate_xml_common_type_header: Error writing attribute (kind) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "type_id", ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_common_type_header: Error writing attribute (type_id) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param channel_id: 
  * @param dsl_color
  * @param channel_name:
  * @param physical_channel: 
  * @param speed:
  * @param parameters:
  * @param comment: the comment that will be added in the header
  *
  * Generates tags related to channel informations
  */
void XmlOutputWriter::generate_xml_channel (int opcode, UNV_SHORT& channel_id, UNV_SHORT& dsl_color, char* channel_name, 
											char* physical_channel, UNV_LONG& speed, char* parameters, char* comment, char* prot_name) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_channel", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <channel id=".." name="..">
	if (xmlTextWriterStartElement(writer, BAD_CAST "channel") < 0)
		cerr << "generate_xml_channel: Error writing new tag (channel) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(unvShortToCharPtr(&channel_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_channel: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(channel_name, MY_ENCODING)) < 0)
		cerr << "generate_xml_channel: Error writing attribute (name) in xml intermediate file" << endl;
	// <protocol_name>...</protocol_name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "protocol_name") < 0)
		cerr << "generate_xml_channel: Error writing new tag (protocol_name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(prot_name, MY_ENCODING)) < 0)
		cerr << "generate_xml_channel: Error writing text (protocol_name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <physical_channel>...</physical_channel>
	if (xmlTextWriterStartElement(writer, BAD_CAST "physical_channel") < 0)
		cerr << "generate_xml_channel: Error writing new tag (physical_channel) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(physical_channel, MY_ENCODING)) < 0)
		cerr << "generate_xml_channel: Error writing text (physical_channel_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <parameters>...</parameters>
	if (xmlTextWriterStartElement(writer, BAD_CAST "parameters") < 0)
		cerr << "generate_xml_channel: Error writing new tag (parameters) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(parameters, MY_ENCODING)) < 0)
		cerr << "generate_xml_channel: Error writing text (parameters_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <speed>...</speed>
	if (xmlTextWriterStartElement(writer, BAD_CAST "speed") < 0) 
		cerr << "generate_xml_channel: Error writing new tag (speed) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&speed), MY_ENCODING)) < 0)
		cerr << "generate_xml_channel: Error writing text (speed_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <dsl_color>...</dsl_color>
	if (xmlTextWriterStartElement(writer, BAD_CAST "dsl_color") < 0) 
		cerr << "generate_xml_channel: Error writing new tag (dsl_color) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&dsl_color), MY_ENCODING)) < 0) 
		cerr << "generate_xml_channel: Error writing text (dsl_color_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// </channel>
	generate_xml_tag_end();
}

/** 
  * @param name: the program name
  * @param opcode: the opcode id
  * @param comment: the comment that will be added in the header
  *
  * Generates the begining of the program block (opening tag & header block)
  */
void XmlOutputWriter::generate_xml_program_begin (char* name, int opcode, char* comment) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_program", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <program name="..">
	if (xmlTextWriterStartElement(writer, BAD_CAST "program") < 0)
		cerr << "generate_xml_program_begin: Error writing new tag (program) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_program_begin: Error writing attribute (name) in xml intermediate file" << endl;
}

/**
  * @param type: the type name
  *
  * Generates the opening tag of type
  */
void XmlOutputWriter::generate_xml_type_begin (char* type) {
	// <type name="..">
	if (xmlTextWriterStartElement(writer, BAD_CAST "type") < 0)
		cerr << "generate_xml_type_simple_type: Error writing new tag (type) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(type, MY_ENCODING)) < 0)
		cerr << "generate_xml_type_simple_type: Error writing attribute (name) in xml intermediate file" << endl;
}

/**
  * @param opcode: the opcode id
  * @param name: 
  * @param field_id:
  * @param type_id: 
  * @param comment: the comment that will be added in the header block
  *
  * Generates the block 'struct_field' with the given informations
  */
void XmlOutputWriter::generate_xml_struct_field (int opcode, char* name, UNV_LONG& field_id, UNV_LONG& type_id, char* comment) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_type", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <type name="struct field">
	generate_xml_type_begin("struct field");
	// <common_type_header ... />
	generate_xml_common_type_header (name, KIND_OF_TYPE_STRUCTURE, type_id);
	// <name>...</name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "name") < 0)
		cerr << "generate_xml_struct_field: Error writing new tag (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_struct_field: Error writing text (name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <type_id>...</type_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "type_id") < 0)
		cerr << "generate_xml_struct_field: Error writing new tag (type_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_struct_field: Error writing text (type_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <field_id>...</field_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "field_id") < 0)
		cerr << "generate_xml_struct_field: Error writing new tag (field_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&field_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_struct_field: Error writing text (field_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// </type>
	generate_xml_tag_end();
}

/**
  * @param kind_of_simple_type: 
  *
  * Generates the tags for a simple type
  */
void XmlOutputWriter::generate_xml_type_simple_type (int kind_of_simple_type) {
	/*
		* generate_xml_type_begin will be called from sltype.cxx:951
		* generate_xml_header & generate_xml_common_type_header will be called from
			sltype.cxx:355
	*/
	
	// <kind_of_simple_type id="..">...</kind_of_simple_type>
	if (xmlTextWriterStartElement(writer, BAD_CAST "kind_of_simple_type") < 0)
		cerr << "generate_xml_type_simple_type: Error writing new tag (kind_of_simple_type) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(intToCharPtr(kind_of_simple_type), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_simple_type: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(getSimpleTypeText(kind_of_simple_type), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_simple_type: Error writing text (kind_of_simple_type_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param field_count: the number of field in the structure
  *
  * Generate the tags for a struct type
  */
void XmlOutputWriter::generate_xml_type_struct (UNV_SHORT& field_count) {
	/*
		* generate_xml_type_begin will be called from sltype.cxx:1276
		* generate_xml_type_end will be called from sltype.cxx:1276 (end of function)
		* generate_xml_struct_type will be called from sltype.cxx:1276 (inside while loop)
		* generate_xml_header & generate_xml_common_type_header will be called from
			sltype.cxx:355
	*/
	
	// <field_count>...</type_count>
	if (xmlTextWriterStartElement(writer, BAD_CAST "field_count") < 0)
		cerr << "generate_xml_type_struct: Error writing new tag (field_count) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&field_count), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_struct: Error writing text (field_count_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param type_id:
  * @param from: ??
  * @param till: ??
  *
  * Generates the tags for an array type
  */
void XmlOutputWriter::generate_xml_type_array (UNV_LONG& type_id, UNV_LONG& from, UNV_LONG& till) {
	/*
		* generate_xml_type_begin will be called from sltype.cxx:1329
		* generate_xml_header & generate_xml_common_type_header will be called from
			sltype.cxx:355
	*/
	
	// <element_type_id>...</element_type_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "element_type_id") < 0)
		cerr << "generate_xml_type_array: Error writing new tag (element_type_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_array: Error writing text (element_type_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <from>...</from>
	if (xmlTextWriterStartElement(writer, BAD_CAST "from") < 0)
		cerr << "generate_xml_type_array: Error writing new tag (from) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&from), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_array: Error writing text (from_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <till>...</till>
	if (xmlTextWriterStartElement(writer, BAD_CAST "till") < 0)
		cerr << "generate_xml_type_array: Error writing new tag (till) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&till), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_array: Error writing text (till_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param type_id: 
  * @param element_count: the number of elements in the list
  *
  * Generates the tags for a list type
  */
void XmlOutputWriter::generate_xml_type_list (UNV_LONG& type_id, UNV_LONG& element_count) {
	/*
		* generate_xml_type_begin will be called from sltype.cxx:1453
		* generate_xml_header & generate_xml_common_type_header will be called from
			sltype.cxx:355
	*/
	
	// <element_type_id>...</element_type_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "element_type_id") < 0)
		cerr << "generate_xml_type_list: Error writing new tag (element_type_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_list: Error writing text (element_type_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <element_count>...</element_count>
	if (xmlTextWriterStartElement(writer, BAD_CAST "element_count") < 0)
		cerr << "generate_xml_type_list: Error writing new tag (element_count) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&element_count), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_list: Error writing text (element_count_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param type_id: 
  *
  * Generates the tags for a pointer or derived type
  */
void XmlOutputWriter::generate_xml_type_pointer_or_derived (UNV_LONG& type_id) {
	/*
		* generate_xml_type_begin will be called from sltype.cxx:1493 | 1817
		* generate_xml_header & generate_xml_common_type_header will be called from
			sltype.cxx:355
	*/
	
	// <element_type_id>...</element_type_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "element_type_id") < 0)
		cerr << "generate_xml_type_pointer_or_derived: Error writing new tag (element_type_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_type_pointer_or_derived: Error writing text (element_type_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param name: the variable name
  * @param type_id: the variable type id
  * @param var_id: 
  * @param father_var_id: 
  * @param element_from_father: ??
  * @param dsl_color: the color assigned to the variable for the distribution
  * @param driver_id: ??
  * @param channel_id: 
  * @param scope_type: 
  * @param in_or_out: input variable or output variable
  * @param address: ??
  * @param comment: the comment that will be added in the header
  *
  * Generates the tags with the giver informations for a variable
  */
void XmlOutputWriter::generate_xml_variables (int opcode, char* name, DSL_TYPE_ID& type_id, UNV_DIC_VAR_ID& var_id,
								UNV_DIC_VAR_ID& father_var_id, UNV_LONG& element_from_father, UNV_SHORT& dsl_color,
								UNV_LONG& driver_id, UNV_SHORT& channel_id, int scope_type, 
								int in_or_out, char* address, char* comment) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_variable", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <variable>
	if (xmlTextWriterStartElement(writer, BAD_CAST "variable") < 0)
		cerr << "generate_xml_variables: Error writing new tag (variable) in xml intermediate file" << endl;
	// <name>...</name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "name") < 0)
		cerr << "generate_xml_variables: Error writing new tag (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <type_id>...</type_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "type_id") < 0)
		cerr << "generate_xml_variables: Error writing new tag (type_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (type_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <variable_id>...</variable_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "variable_id") < 0)
		cerr << "generate_xml_variables: Error writing new tag (variable_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&var_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (variable_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <father_variable_id>...</father_variable_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "father_variable_id") < 0)
		cerr << "generate_xml_variables: Error writing new tag (father_variable_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&father_var_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (father_variable_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <element_from_father>...</element_from_father>
	if (xmlTextWriterStartElement(writer, BAD_CAST "element_from_father") < 0)
		cerr << "generate_xml_variables: Error writing new tag (element_from_father) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&element_from_father), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (element_from_father_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <driver_id>...</driver_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "driver_id") < 0)
		cerr << "generate_xml_variables: Error writing new tag (driver_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&driver_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (driver_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <channel_id>...</channel_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "channel_id") < 0)
		cerr << "generate_xml_variables: Error writing new tag (channel_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&channel_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (channel_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <input_or_output>...</input_or_output>
	if (xmlTextWriterStartElement(writer, BAD_CAST "input_or_output") < 0)
		cerr << "generate_xml_variables: Error writing new tag (input_or_output) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(intToCharPtr(in_or_out), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (input_or_output_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <scope_type>...</scope_type>
	if (xmlTextWriterStartElement(writer, BAD_CAST "scope_type") < 0)
		cerr << "generate_xml_variables: Error writing new tag (scope_type) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(intToCharPtr(scope_type), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (scope_type_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <dsl_color>...</dsl_color>
	if (xmlTextWriterStartElement(writer, BAD_CAST "dsl_color") < 0)
		cerr << "generate_xml_variables: Error writing new tag (dsl_color) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&dsl_color), MY_ENCODING)) < 0)
		cerr << "generate_xml_variables: Error writing text (dsl_color_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <address>...</address>
	if (xmlTextWriterStartElement(writer, BAD_CAST "address") < 0)
		cerr << "generate_xml_variables: Error writing new tag (address) in xml intermediate file" << endl;
	if (strcmp(address, "") == 0) {
		if (xmlTextWriterWriteString(writer, ConvertInput("0", MY_ENCODING)) < 0)
			cerr << "generate_xml_variables: Error writing text (address_text) in xml intermediate file" << endl;
	}
	else {
		if (xmlTextWriterWriteString(writer, ConvertInput(address, MY_ENCODING)) < 0)
			cerr << "generate_xml_variables: Error writing text (address_text) in xml intermediate file" << endl;
	}
	generate_xml_tag_end();
	// </variable>
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param method_id:
  * @param type_id: 
  * @param name: the method name
  * @param comment: the comment that will be added in the header
  * @param argument_count: the number of arguments for the method
  *
  * Generates tags with given informations for a method
  */
void XmlOutputWriter::generate_xml_method_begin (int opcode, UNV_METHOD_ID& method_id, DSL_TYPE_ID& type_id, char* name, char* comment, UNV_SHORT& argument_count) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_method", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	if (opcode == DSL_INTERMEDIATE_OPCODE_FUNCTION_ENTRY) {
		// <method>
		if (xmlTextWriterStartElement(writer, BAD_CAST "method") < 0)
			cerr << "generate_xml_method: Error writing new tag (method) in xml intermediate file" << endl;
	}
	else if (opcode == DSL_INTERMEDIATE_OPCODE_FORWARD_FUNCTION_ENTRY) {
		// <method_forward>
		if (xmlTextWriterStartElement(writer, BAD_CAST "method_forward") < 0)
			cerr << "generate_xml_method: Error writing new tag (method) in xml intermediate file" << endl;
	}
	// <name>...</name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "name") < 0)
		cerr << "generate_xml_method: Error writing new tag (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_method: Error writing text (name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <method_id>...</method_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "method_id") < 0)
		cerr << "generate_xml_method: Error writing new tag (method_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&method_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_method: Error writing text (method_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <type_id>...</type_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "type_id") < 0)
		cerr << "generate_xml_method: Error writing new tag (type_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&type_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_method: Error writing text (type_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <argument_count>...</argument_count>
	if (xmlTextWriterStartElement(writer, BAD_CAST "argument_count") < 0)
		cerr << "generate_xml_method: Error writing new tag (argument_count) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&argument_count), MY_ENCODING)) < 0)
		cerr << "generate_xml_method: Error writing text (argument_count_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param node_number: ??
  * @param start_line: ??
  *
  * Generates start_node block
  */
void XmlOutputWriter::generate_xml_start_node (int opcode, char* comment, long node_number, UNV_SHORT& start_line) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_start_node", opcode, comment, start_line, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <start_node id=".." />
	if (xmlTextWriterStartElement(writer, BAD_CAST "start_node") < 0)
		cerr << "generate_xml_start_node: Error writing new tag (node) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(longToCharPtr(node_number), MY_ENCODING)) < 0)
		cerr << "generate_xml_start_node: Error writing new attribute (id) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param comment: the comment that will be added in the header
  * @param instruction: ??
  * (other params are line numbers and positions of instructions in source code
  *
  * Generates tags with given informations for an instruction
  */
void XmlOutputWriter::generate_xml_instructions (int opcode, char* comment, UNV_SHORT& start_1, UNV_SHORT& start_2, 
								UNV_SHORT& end_1, UNV_SHORT& end_2, UNV_LONG& pbegin_1, UNV_LONG& pbegin_2, 
								UNV_LONG& pend_1, UNV_LONG& pend_2, int instruction) {
	// <header>...</header>
	generate_xml_header("header_instruction", opcode, comment, start_1, end_1, start_2, end_2, pbegin_1, pend_1, pbegin_2, pend_2);
	// <instruction id=".." text="..">
	if (xmlTextWriterStartElement(writer, BAD_CAST "instruction") < 0)
		cerr << "generate_xml_instructions: Error writing new tag (instruction) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(intToCharPtr(instruction), MY_ENCODING)) < 0)
		cerr << "generate_xml_instructions: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "text", ConvertInput(DSL_INSTRUCTIONS_NAME[instruction], MY_ENCODING)) < 0)
		cerr << "generate_xml_instructions: Error writing text (instr_text) in xml intermediate file" << endl;
}

/**
  * @param arg_count the number of arguments
  *
  * Adds a tag argument_count to an instruction block
  */
void XmlOutputWriter::generate_xml_instructions_method (int arg_count) {
	// <argument_count>...</argument_count>
	if (xmlTextWriterStartElement(writer, BAD_CAST "argument_count") < 0)
		cerr << "generate_xml_instruction_method: Error writing new tag (argument_count) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(intToCharPtr(arg_count), MY_ENCODING)) < 0)
		cerr << "generate_xml_instructions_method: Error writing text (argument_count_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param node
  * @param node_name
  *
  * Generates tag node with given information
  */
void XmlOutputWriter::generate_xml_node (long node, char* node_name) {
	// <node name=".." id="..">...</node>
	if (xmlTextWriterStartElement(writer, BAD_CAST "node") < 0)
		cerr << "generate_xml_node: Error writing new tag (node) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(node_name, MY_ENCODING)) < 0)
		cerr << "generate_xml_node: Error writing attribute (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(longToCharPtr(node), MY_ENCODING)) < 0)
		cerr << "generate_xml_node: Error writing text (node_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param comment: 
  *
  * Generates tags with given information for trigger
  */
void XmlOutputWriter::generate_xml_trigger (int opcode, char* comment) {
	// <header>...</header>
	UNV_LONG l_no_val;
	UNV_SHORT s_no_val;
	assign_unv_long(&l_no_val, 0);
	assign_unv_short(&s_no_val, 0);
	generate_xml_header("header_trigger", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <trigger>
	if (xmlTextWriterStartElement(writer, BAD_CAST "trigger") < 0)
		cerr << "generate_xml_trigger: Error writing new tag (trigger) in xml intermediate file" << endl;
	// </trigger>
	generate_xml_tag_end();
}

/**
  * @param elem_count: 
  *
  * Generates tags with given information for a case statement
  */
void XmlOutputWriter::generate_xml_case_statement_begin (long elem_count) {
	// <elements nb="..">
	if (xmlTextWriterStartElement(writer, BAD_CAST "elements") < 0)
		cerr << "generate_xml_case_statement_begin: Error writing new tag (elements) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "nb", ConvertInput(longToCharPtr(elem_count), MY_ENCODING)) < 0)
		cerr << "generate_xml_case_statement_begin: Error writing attribute (nb_elem) in xml intermediate file" << endl;
}

/**
  * @param node: 
  * @param value_from: 
  * @param value_to: 
  *
  * Generates tags with given information for elements in case statements
  */
void XmlOutputWriter::generate_xml_case_statement_elems(long node, short value_from, short value_to) {
	// <node>...</node>
	generate_xml_node(node, "case_statement");
	// <value from=".." to=".." />
	if (xmlTextWriterStartElement(writer, BAD_CAST "value") < 0)
		cerr << "generate_xml_case_statement_elems: Error writing new tag (value) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "from", ConvertInput(intToCharPtr(value_from), MY_ENCODING)) < 0)
		cerr << "generate_xml_case_statement_elems: Error writing attribute (from) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "to", ConvertInput(intToCharPtr(value_to), MY_ENCODING)) < 0)
		cerr << "generate_xml_case_statement_elems: Error writing attribute (to) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param method_id: 
  * @param launch: 
  * @param params: 
  *
  * Generates tags with given information for method call
  */
void XmlOutputWriter::generate_xml_method_call(UNV_LONG& method_id, int launch, long params) {
	// <method_id>...</method_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "method_id") < 0)
		cerr << "generate_xml_method_call: Error writing new tag (method_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&method_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_method_call: Error writing text (method_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <is_launch>...</is_launch>
	if (xmlTextWriterStartElement(writer, BAD_CAST "is_launch") < 0)
		cerr << "generate_xml_method_call: Error writing new tag (launch) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(intToCharPtr(launch), MY_ENCODING)) < 0)
		cerr << "generate_xml_method_call: Error writing text (launch_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <param_count>...</param_count>
	if (xmlTextWriterStartElement(writer, BAD_CAST "param_count") < 0)
		cerr << "generate_xml_method_call: Error writing new tag (param_count) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(longToCharPtr(params), MY_ENCODING)) < 0)
		cerr << "generate_xml_method_call: Error writing text (param_count_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param name: 
  *
  * Generates tags with given information for plugin statement
  */
void XmlOutputWriter::generate_xml_statements_plugin_name(char* name) {
	// <plugin name=".." />
	if (xmlTextWriterStartElement(writer, BAD_CAST "plugin") < 0)
		cerr << "generate_xml_statements_plugin_name: Error writing new tag (plugin) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "name", ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_statements_plugin_name: Error writing attribute (name) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param comment: the comment that will be added in the header
  * @param operation: ??
  * @param special_tag: the tag name of the specific information 
  * @param special_value: the value of that specific tag
  * @param tag_name: is used to differenciate operation_lhs and operation_rhs
  *
  * Generates tags with given informations for an operation
  */
void XmlOutputWriter::generate_xml_operation (int opcode, char* comment, UNV_SHORT& start_1, UNV_SHORT& start_2, UNV_SHORT& end_1, UNV_SHORT& end_2, 
												UNV_LONG& pbegin_1, UNV_LONG& pbegin_2, UNV_LONG& pend_1, UNV_LONG& pend_2, int operation) {
	// <header>...</header>
	generate_xml_header("header_operation", opcode, comment, start_1, end_1, start_2, end_2, pbegin_1, pend_1, pbegin_2, pend_2);
	// <operation>
	if (xmlTextWriterStartElement(writer, BAD_CAST "operation") < 0)
		cerr << "generate_xml_operation: Error writing new tag (operation) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(intToCharPtr(operation), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "text", ConvertInput(DSL_OPERATIONS_NAME[operation], MY_ENCODING)) < 0)
		cerr << "generate_xml_operation: Error writing text (op_text) in xml intermediate file" << endl;
}

/**
  * @param op: the operator 
  *
  * Generates tags operator for for_loops
  */
void XmlOutputWriter::generate_xml_operation_operator (int op) {
	// <operator id="..">...</operator>
	if (xmlTextWriterStartElement(writer, BAD_CAST "operator") < 0)
		cerr << "generate_xml_operation_operator: Error writing new tag (operator) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(intToCharPtr(op), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_operator: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(DSL_OPERATORS_NAME[op], MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_operator: Error writing text (operator_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param stack_offset: 
  *
  * Generates the specific tag with given information for select stack
  */
void XmlOutputWriter::generate_xml_operation_select_stack (UNV_LONG& stack_offset) {
	// <stack_offset>...</stack_offset>
	if (xmlTextWriterStartElement(writer, BAD_CAST "stack_offset") < 0)
		cerr << "generate_xml_operation_select_stack: Error writing new tag (stack_offset) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&stack_offset), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_select_stack: Error writing text (stack_offset_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param name: 
  * @param result_type: 
  * @param param_count: 
  *
  * Generates the specific tags with given information for function call
  */
void XmlOutputWriter::generate_xml_operation_function_call (char* name, int result_type, UNV_SHORT& param_count) {
	// <name>...</name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "name") < 0)
		cerr << "generate_xml_operation_function_call: Error writing new tag (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_function_call: Error writing text (name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <result_type>...</result_type>
	if (xmlTextWriterStartElement(writer, BAD_CAST "result_type") < 0)
		cerr << "generate_xml_operation_function_call: Error writing new tag (result_type) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(intToCharPtr(result_type), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_function_call: Error writing text (result_type_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <param_count>...</param_count>
	if (xmlTextWriterStartElement(writer, BAD_CAST "param_count") < 0)
		cerr << "generate_xml_operation_function_call: Error writing new tag (param_count) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&param_count), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_function_call: Error writing text (param_count_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param field_id: 
  *
  * Generates the specific tags with given information for select struct
  */
void XmlOutputWriter::generate_xml_operation_select_struct (UNV_LONG& field_id) {
	// <field_id>...</field_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "field_id") < 0)
		cerr << "generate_xml_operation_select_struct: Error writing new tag (field_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&field_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_select_struct: Error writing text (field_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param length: 
  * @param kind_of_type: 
  * 
  * Generates the specific tags with given information for constants
  */
void XmlOutputWriter::generate_xml_operation_value_constant (UNV_SHORT& length, char kind_of_type[1024]) {
	// <length>...</length>
	if (xmlTextWriterStartElement(writer, BAD_CAST "length") < 0)
		cerr << "generate_xml_operation_value_constant: Error writing new tag (length) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvShortToCharPtr(&length), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_value_constant: Error writing text (length_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	for (int i = 0; i < atoi(unvShortToCharPtr(&length)); i++) {
		// <value>...</value>
		if (xmlTextWriterStartElement(writer, BAD_CAST "value") < 0) 
			cerr << "generate_xml_operation_value_constant: Error writing new tag (value) in xml intermediate file" << endl;
		if (xmlTextWriterWriteString(writer, ConvertInput(intToCharPtr(kind_of_type[i]), MY_ENCODING)) < 0)
			cerr << "generate_xml_operation_value_constant: Error writing text (value_text) in xml intermediate file" << endl;
		if (xmlTextWriterEndElement(writer) < 0) 
			cerr << "generate_xml_operation_value_constant: Error closing tag (value) in xml intermediate file" << endl;
	}
	/* PREVIOUS VERSION
	// <buffer value_i=".." ... />
	if (xmlTextWriterStartElement(writer, BAD_CAST "buffer") < 0)
		cerr << "generate_xml_operation_value_constant: Error writing new tag (buffer) in xml intermediate file" << endl;
	char tag_name[20];
	for (int i = 0; i < atoi(unvShortToCharPtr(&length)); i++) {
		sprintf(tag_name, "value_%d", i);
		if (xmlTextWriterWriteAttribute(writer, ConvertInput(tag_name, MY_ENCODING), ConvertInput(intToCharPtr(kind_of_type[i]), MY_ENCODING)) < 0)
			cerr << "generate_xml_operation_value_constant: Error writing attribute (value) in xml intermediate file" << endl;
	}
	generate_xml_tag_end();*/
}

/**
  * @param var_id: 
  *
  * Generates the specific tags with given information for volatile vars
  */
void XmlOutputWriter::generate_xml_operation_volatile_var (UNV_LONG& var_id) {
	// <var_id>...</var_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "var_id") < 0)
		cerr << "generate_xml_operation_volatile_var: Error writing new tag (var_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&var_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_operation_volatile_var: Error writing text (var_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param comment: the comment that will be added in the header
  * @param name: the sequence name
  * @param async: ??
  *
  * Generates tags with given informations for a sequence
  */
void XmlOutputWriter::generate_xml_sequence (int opcode, char* comment, char* name, int async) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_sequence", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <sequence>
	if (xmlTextWriterStartElement(writer, BAD_CAST "sequence") < 0)
		cerr << "generate_xml_sequence: Error writing new tag (sequence) in xml intermediate file" << endl;
	// <name>...</name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "name") < 0)
		cerr << "generate_xml_sequence: Error writing new tag (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_sequence: Error writing text (name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <async>...</async>
	if (xmlTextWriterStartElement(writer, BAD_CAST "async") < 0)
		cerr << "generate_xml_sequence: Error writing new tag (async) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(charToCharPtr(async), MY_ENCODING)) < 0)
		cerr << "generate_xml_sequence: Error writing text (async_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param opcode: the opcode id
  * @param comment: the comment that will be added in the header
  * @param cfg_id: 
  * @param kind_of_cfg: ??
  * @param name: ?
  *
  * Generates tags with given informations for a statement  
  */
void XmlOutputWriter::generate_xml_statements (int opcode, char* comment, UNV_CFG_ID& cfg_id, int kind_of_cfg, char* name) {
	// <header>...</header>
	UNV_SHORT s_no_val;
	UNV_LONG l_no_val;
	assign_unv_short(&s_no_val, 0);
	assign_unv_long(&l_no_val, 0);
	generate_xml_header("header_statements", opcode, comment, s_no_val, s_no_val, s_no_val, s_no_val, l_no_val, l_no_val, l_no_val, l_no_val);
	// <statements>
	if (xmlTextWriterStartElement(writer, BAD_CAST "statements") < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing new tag (statements) in xml intermediate file" << endl;
	// <cfg_id>...</cfg_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "cfg_id") < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing new tag (cfg_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&cfg_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing text (cfg_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <kind_of_cfg id="..">...</kind_of_cfg>
	if (xmlTextWriterStartElement(writer, BAD_CAST "kind_of_cfg") < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing new tag (kind_of_cfg) in xml intermediate file" << endl;
	if (xmlTextWriterWriteAttribute(writer, BAD_CAST "id", ConvertInput(intToCharPtr(kind_of_cfg), MY_ENCODING)) < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing attribute (id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(DSL_KIND_CFG_NAME[kind_of_cfg], MY_ENCODING)) < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing text (kind_of_cfg_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
	// <name>...</name>
	if (xmlTextWriterStartElement(writer, BAD_CAST "name") < 0)
		cerr << "generate_xml_methods_whens_statements: Error writing new tag (name) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(name, MY_ENCODING)) < 0)
		cerr << "generate_xml_methods_whens_statements: Error writin text (name_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}

/**
  * @param var_id: 
  *
  * Generates tags self_var_id with given informations
  */
void XmlOutputWriter::generate_xml_statements_self (UNV_LONG& var_id) {
	// <self_var_id>...</self_var_id>
	if (xmlTextWriterStartElement(writer, BAD_CAST "self_var_id") < 0)
		cerr << "generate_xml_statements_self: Error writing new tag (self_var_id) in xml intermediate file" << endl;
	if (xmlTextWriterWriteString(writer, ConvertInput(unvLongToCharPtr(&var_id), MY_ENCODING)) < 0)
		cerr << "generate_xml_statements_self: Error writing text (var_id_text) in xml intermediate file" << endl;
	generate_xml_tag_end();
}
